// helpers.js

/**
 * 根据给定的 key 值，在 routes 数组中查找匹配项或子项
 * @param {string} key - 要查找的 key 值
 * @param {Array} key - 路由数组
 * @returns {object} - 匹配到的路由配置项，如果没有匹配到则返回 null
 */
export const findRouteByKey = (key, routes) => {
    let matchedRoute = null;
    routes.forEach(element => {
        if (element.key === key) matchedRoute = element;
        else if (element.children) {
            element.children.forEach(child => {
                if (child.key === key) matchedRoute = child;
            });
        }
    });

    return matchedRoute || null;
};

/**
 * 获取当前页面所属的模块key
 * 注意：该方法待测试
 * @returns {string} 当前模块的key
 */
export const getCurrentModuleKey = routes => {
    const path = window.location.pathname;
    const [, content] = path.match(/^\/([^/]+)\/[^/]+/) || [];
    const route = findRouteByKey(content, routes);
    return route ? route.key : 0;
};
